package com.yexiong.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.Payment;
import com.yexiong.dao.IPayDao;

public class PayDaoImpl extends BaseDao implements IPayDao{

	@Override
	public int insert(Payment payment) {
		int reslut=0;
		String sql="insert into payment_documents(itemNumber,receivable,collected,documentDate,"
				+ "coachNumber,pay_type_id,remark) values(?,?,?,?,?,?,?)";
		Object[] objects= {payment.getPayItemNumber(),payment.getPayReceivable(),payment.getPayCollected()
				,payment.getPayDate(),payment.getPayCoachNumber(),payment.getPayTypeId(),payment.getPayRemark()};
		reslut=excuteUpdate(sql, objects);
		return reslut;
	}

	@Override
	public List<Payment> getOnePageInfo(String condition, int currentPage, int pageSize, String startTime,
			String endTime) {
		List<Payment> list=new ArrayList<>();
		String sql="select * from payment_documents where coachNumber like ? and documentDate BETWEEN ? AND ? limit ?,?";
		Object[] objects= {"%"+condition+"%",startTime,endTime,(currentPage-1)*pageSize,pageSize};
		list=extuteQuery(sql, objects);
		
		return list;
	}

	@Override
	public int getCount(String startTime,String endTime,String condition) {
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		int count=-1;
		String sql="select count(1) num from payment_documents where coachNumber like ? and documentDate BETWEEN ? AND ? ";
		try {
			connection=getConnection();
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setObject(1,"%"+condition+"%" );
			preparedStatement.setObject(2, startTime);
			preparedStatement.setObject(3, endTime);
			resultSet=preparedStatement.executeQuery();
			if (resultSet.next()) {
				count=resultSet.getInt("num");
			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			closeAll(connection, preparedStatement, resultSet);
		}
		
		return count;
	}

	@Override
	public int deleteByDocumentId(String id) {
		int result=0;
		String sql="delete from payment_documents where documentNum=?";
		result=excuteUpdate(sql, id);
		return result;
	}

	@Override
	public Object getEntity(ResultSet resultSet) {
		Payment payment=new Payment();
		try {
			payment.setDocumentNum(resultSet.getInt("documentNum"));
			payment.setPayAdmiNumber(resultSet.getInt("admiNumber"));
			payment.setPayArrear(resultSet.getDouble("arrear"));
			payment.setPayCoachNumber(resultSet.getInt("coachNumber"));
			payment.setPayCollected(resultSet.getDouble("collected"));
			payment.setPayDate(resultSet.getDate("documentDate"));
			payment.setPayItemNumber(resultSet.getInt("itemNumber"));
			payment.setPayReceivable(resultSet.getDouble("receivable"));
			payment.setPayRefundMoney(resultSet.getDouble("refund_money"));
			payment.setPayRemark(resultSet.getString("remark"));
			payment.setPayStuId(resultSet.getInt("stu_id"));
			payment.setPayTypeId(resultSet.getInt("pay_type_id"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return payment;
	}

	@Override
	public int update(Payment payment) {
		String sql="update payment_documents set pay_type_id=?,receivable=?,collected=?,"
				+ "documentDate=?,remark=? where documentNum=?";
		int result=0;
		Object[] objects= {payment.getPayTypeId(),payment.getPayReceivable(),payment.getPayCollected(),
				payment.getPayDate(),payment.getPayRemark(),payment.getDocumentNum()};
		result=excuteUpdate(sql, objects);
		return result;
	}

}
